package com.kdtech.analyse.Bbs.common;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.ParseState;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;
/**
 * Discuz! 7.2
 *
 * 特征:
<script type="text/javascript">var STYLEID = '3', discuz_uid = 0, cookiedomain = 'mycar168.net', cookiepath = '/', attackevasive = '0', disallowfloat = 'newthread|reply', creditnotice = '1|经验|,2|威望|,3|金钱|￥,4|魅力|,5|点券|￥', gid = parseInt('1'), fid = parseInt('11'), tid = parseInt('388814')</script>
 *
 * 7361
 * http://www.mycar168.net/ 麦卡车会
 * @author KK
 */
public class BbsAnalyseDiscuzV7 implements AnalyseNews{


	
	public boolean isDetailPage(String url) {
		url = UrlArgumentTop.FromatUrl(url);
		url = StringUtils.substringAfterLast(url, "/");
		String[] regex = {
				"viewthread.php[?]tid=[0-9]*&fid=[0-9]*",
				"thread-[0-9]*-1-1.html",
				"viewthread.php[?]extra=page%3D1&tid=[0-9]*",
				"viewthread.php[?]tid=[0-9]*",
				"viewthread.php[?]tid=[0-9]*&extra=page%3D1",
				"forum.php\\?mod=viewthread&tid=[0-9]*",
				"viewthread.php\\?tid=[0-9]*&extra=&page=1",
				"viewthread.php\\?extra=.*&page=1&tid=[0-9]*",
				"forum.php\\?extra=.*&mod=viewthread&tid=[0-9]*",
				"viewthread.php\\?extra=[^&]+&tid=[0-9]+",
				"[0-9]+-1-1.html",
				"[0-9]+-1.html",
				"t-[0-9]+-1-1.html"
				};
		return RegexUtils.matchAny(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {

		if (urlMeta.getHtml() == null) {
			return null;
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();

		String title = null;
		String content = null;
		Long date = null;
		Integer commnetNum = null;
		Integer clickNum = null;
		String author=null;
		String updateUrl = url;
		Document doc = Jsoup.parse(htmltxt);
		doc.select("a:containsOwn(登录/注册后可看大图)").remove();
		NewsMeta news = new NewsMeta();
		doc.select(".attach_nopermission").remove();
		if(doc.select("div.alert_error").size() > 0){
			news.setUpdateUrl(ParseState.ERR404.toString());
			return news;
		}
		title = doc.select("#thread_subject").text();
		if(StringUtils.isBlank(title)){
			title = doc.select("title").text();
			if (title!=null){
				title = StringUtils.substringBefore(title, " - ");
			}
		}

		if(StringUtils.isBlank(title))
			title = doc.select("h1.ts").text();
		Element firstDiv = doc.select(" div[id^=post_]").first();
		if (firstDiv!=null){
			date = DateUtils.matchDate(firstDiv.select("div.authorinfo span[title]").attr("title"));
			if (date==null){
				date = DateUtils.matchDate(firstDiv.select("div.authorinfo").text().replace(" ", ""));
			}
			if (date==null){
				date = DateUtils.matchDate(firstDiv.select(".fbtime").text());
			}
			content = HtmlCleaner.getContentHtml(url,firstDiv.select(".t_msgfont"));
			if(StringUtils.isBlank(content)){
				content = HtmlCleaner.getContentHtml(url,doc.select("td.t_msgfont"));
			}
			if(StringUtils.isBlank(content)){
				content = HtmlCleaner.getContentHtml(url,doc.select("td.t_f"));
			}
			if(StringUtils.isBlank(content)){
				Elements select = doc.select(".t_f");
				content = HtmlCleaner.getContentHtml(url,select.first());
			}
			author = firstDiv.select("td.postauthor div.postinfo a").text();
			if(StringUtils.isBlank(author)){
				author = firstDiv.select("h5.user_name").text();
			}
			if(StringUtils.isBlank(author)){
				author = firstDiv.select("div.user-pic").text();
			}
			if(StringUtils.isBlank(author)){
				author = firstDiv.select(".client_n").text();
			}
			if(StringUtils.isBlank(author)){
				author = firstDiv.select(".vm a[href^=home.php]").text();
			}
			if (StringUtils.isNullOrEmpty(author)){
				author = firstDiv.select("a.posterlink").text();
				if (StringUtils.isNullOrEmpty(author)){
					Elements select = firstDiv.select(".authi");
					if (select.size()>0){
						author = firstDiv.select(".authi").get(0).text();
					}
				}
			}
		}

		if(StringUtils.isBlank(author)){
			Elements select = doc.select("div.authi");
			if(select!=null && select.size()>0){
				author =  select.get(0).select("a.xw1").text();
				if(StringUtils.isBlank(author)){
					author = select.get(0).select("a.xi2").text();
				}
			}
		}

		if(date == null){
			date =DateUtils.matchDate(doc.select(".authi").text());
		}

		String commTxt = doc.select("td.plc span.y").text();
		commnetNum = NumberUtils.matchNumber(StringUtils.substringBefore(commTxt, "|"));
		clickNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "|"));

		if(commnetNum==null && clickNum==null){
			Element first = doc.select("div.posts_info span.txt").first();
			if(first!=null) commTxt = first.text();
			commnetNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "回复"));
			clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(commTxt, "回复"));
		}

		if(commnetNum==null && clickNum==null){
			commTxt = doc.select("span.popularity").text();
			commnetNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "|"));
			clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(commTxt, "|"));
		}
		if(commnetNum==null && clickNum==null){
			commTxt = doc.select("ul.view-hd-num").text();
			commnetNum = NumberUtils.matchNumber(StringUtils.substringBetween(commTxt, "查看", "回复"));
			clickNum = NumberUtils.matchNumber(StringUtils.substringBefore(commTxt, "查看"));
		}
		if(commnetNum==null && clickNum==null){
			commnetNum = NumberUtils.matchNumber(doc.select("span.comiis_hfs").text());
			clickNum = NumberUtils.matchNumber(doc.select("span.comiis_cks").text());
		}
		if(commnetNum==null && clickNum==null){
			commTxt = doc.select("em.newviews").text();
			clickNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "浏览"));
			commnetNum = NumberUtils.matchNumber(StringUtils.substringBefore(commTxt, "浏览"));
		}
		if(noNum(commnetNum,clickNum)){
			commTxt = doc.select(".hm").text();
			clickNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "查看"));
			commnetNum = NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "回复"));
		}

			news.setUrl(url);
			news.setType(4);
			news.setTitle(StringUtils.trimSpace(title));
			news.setCommentNum(commnetNum);
			news.setClickNum(clickNum);
			news.setContent(StringUtils.trimSpace(content));
			news.setDate(date);
			news.setAuthor(author);
			news.setUpdateUrl(updateUrl);
			return news;
	}

	private boolean noNum(Integer commnetNum, Integer clickNum) {
		return (commnetNum==null || commnetNum==0) && (clickNum==null || clickNum==0)  ;

	}

	
	public NewsMeta Update(NewsMeta meta) {
		String url = meta.getUpdateUrl();
		if (StringUtils.isNotBlank(url)){
			UrlMeta urlMeta = CrawlHTML.responseToURL(url);
			return parserHtml(urlMeta);
		}
		return null;
	}

	

	
}
